<?php

/**
 * Notes:
 * User: LiaoHong
 * Date: 2025/10/16
 * Time: 16:44
 **/
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\User\UserRequest;
use App\Services\User\UserService;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public UserService $userService;
    public function __construct(UserService $service)
    {
        $this->userService = $service;
    }
    /**
     * @Notes:用户登录
     * @param UserRequest $request
     * @return JsonResponse
     * @author: Liao Hong
     * @Time: 2025/10/21   14:29
     * @Interface login
     */
    public function login(UserRequest $request):JsonResponse
    {
        try {
            $result = $this->userService->login($request->input());
            return success($result, '登陆成功！');
        } catch (AuthenticationException  $e) {
            return fail($e->getMessage());
        }
    }
    /**
     * @Notes:用户退出登录
     * @param Request $request
     * @return JsonResponse
     * @author: Liao Hong
     * @Time: 2025/10/21   14:29
     * @Interface logout
     */
    public function logout(Request $request)
    {
        auth('api')->logout();
        return success('', '登出成功！');
    }
}
